<script setup>
import { ref } from 'vue'

const emit = defineEmits(['search'])

const searchQuery = ref('')

const handleSearch = () => {
  emit('search', searchQuery.value)
}

const clearSearch = () => {
  searchQuery.value = ''
  emit('search', '')
}
</script>

<template>
  <div class="search-box">
    <div class="search-section">
      <div class="search-input-group">
        <div class="search-input-wrapper">
          <svg class="search-icon" viewBox="0 0 24 24" fill="currentColor">
            <path d="M9.5,3A6.5,6.5 0 0,1 16,9.5C16,11.11 15.41,12.59 14.44,13.73L14.71,14H15.5L20.5,19L19,20.5L14,15.5V14.71L13.73,14.44C12.59,15.41 11.11,16 9.5,16A6.5,6.5 0 0,1 3,9.5A6.5,6.5 0 0,1 9.5,3M9.5,5C7,5 5,7 5,9.5C5,12 7,14 9.5,14C12,14 14,12 14,9.5C14,7 12,5 9.5,5Z" />
          </svg>
          <input 
            v-model="searchQuery"
            @input="handleSearch"
            @compositionend="handleSearch"
            @keyup.enter="handleSearch"
            class="search-input"
            type="text"
            placeholder="搜索文件名..."
          />
          <button 
            v-if="searchQuery"
            @click="clearSearch" 
            class="clear-btn"
            title="清除搜索"
          >
            <svg class="icon" viewBox="0 0 24 24" fill="currentColor">
              <path d="M19,6.41L17.59,5L12,10.59L6.41,5L5,6.41L10.59,12L5,17.59L6.41,19L12,13.41L17.59,19L19,17.59L13.41,12L19,6.41Z" />
            </svg>
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.search-box {
  padding: 16px;
  border-bottom: 1px solid var(--border-color);
  background: var(--search-bg);
}

.light-theme .search-box {
  --border-color: #e0e0e0;
  --search-bg: #ffffff;
}

.dark-theme .search-box {
  --border-color: #333333;
  --search-bg: #1a1a1a;
}



.search-input-group {
  position: relative;
}

.search-input-wrapper {
  position: relative;
  display: flex;
  align-items: center;
}

.search-icon {
  position: absolute;
  left: 12px;
  width: 16px;
  height: 16px;
  color: var(--icon-color);
  pointer-events: none;
  z-index: 1;
}

.search-input {
  width: 100%;
  height: 40px;
  padding: 0 40px 0 36px;
  border: 1px solid var(--input-border);
  border-radius: 8px;
  background: var(--input-bg);
  color: var(--input-color);
  font-size: 14px;
  transition: all 0.2s ease;
}

.search-input:focus {
  outline: none;
  border-color: var(--focus-border);
  box-shadow: 0 0 0 2px var(--focus-shadow);
}

.clear-btn {
  position: absolute;
  right: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  border: none;
  border-radius: 4px;
  background: transparent;
  color: var(--icon-color);
  cursor: pointer;
  transition: all 0.2s ease;
}

.clear-btn:hover {
  background: var(--button-hover-bg);
}

.icon {
  width: 16px;
  height: 16px;
}

/* Light theme variables */
.light-theme .path-input,
.light-theme .search-input {
  --input-border: #d1d5db;
  --input-bg: #ffffff;
  --input-color: #374151;
  --focus-border: #3b82f6;
  --focus-shadow: rgba(59, 130, 246, 0.1);
}

.light-theme .folder-btn,
.light-theme .clear-btn {
  --button-bg: #f9fafb;
  --button-color: #6b7280;
  --button-hover-bg: #f3f4f6;
  --icon-color: #6b7280;
}

/* Dark theme variables */
.dark-theme .path-input,
.dark-theme .search-input {
  --input-border: #4b5563;
  --input-bg: #374151;
  --input-color: #f9fafb;
  --focus-border: #60a5fa;
  --focus-shadow: rgba(96, 165, 250, 0.1);
}

.dark-theme .folder-btn,
.dark-theme .clear-btn {
  --button-bg: #4b5563;
  --button-color: #d1d5db;
  --button-hover-bg: #6b7280;
  --icon-color: #d1d5db;
}
</style>